<template>
	<el-form size="medium" ref="tableForm" :model="tableForm" class="custom_select_wrapper">
		<el-table :data="tableForm.lists" size="medium" border>
			<el-table-column label="年份" width="180">
				<template slot-scope="{ row, $index }">
					<el-form-item :prop="`lists.${$index}.checkYear`" :rules="tableRules.checkYear">
						<el-date-picker v-model="row.checkYear" type="year" placeholder="选择年份"></el-date-picker>
					</el-form-item>
				</template>
			</el-table-column>
			<el-table-column label="考核等次" min-width="300">
				<template slot-scope="{ row, $index }">
					<el-form-item :prop="`lists.${$index}.checkLevel`" :rules="tableRules.checkLevel">
						<el-radio-group v-model="row.checkLevel">
							<el-radio v-for="(item, index) in dict.type.people_check_level" :key="index" :label="item.value">{{ item.label }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</template>
			</el-table-column>
			<el-table-column label="操作" width="48" align="center">
				<template slot-scope="{ row, $index }">
					<el-button class="del" type="text" size="small" icon="el-icon-delete" @click="handleDeleteUser($index)"></el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-button type="primary" size="small" class="add" @click="handleAddedUser()">新增一位</el-button>
	</el-form>
</template>

<script>
import emitter from 'element-ui/src/mixins/emitter';
export default {
	mixins: [emitter],
	props: {
		value: { type: Array, default: () => [] },
		list: { type: Array, default: () => [] }
	},
	dicts: ['people_check_level'],
	data() {
		return {
			tableForm: {
				lists: this.value
			},
			tableRules: {
				checkYear: [{ required: true, message: '请选择年份', trigger: 'change' }],
				checkLevel: [{ required: true, message: '请选择考核等次', trigger: 'change' }]
			}
		};
	},
	watch: {
		value(arr) {
			this.$set(this.tableForm, 'lists', arr);
		},
		'tableForm.lists'(arr) {
			this.$emit('input', arr);
			this.dispatch('ElFormItem', 'el.form.change', [arr]);
		}
	},
	methods: {
		async handleAddedUser() {
			if (!(await this.validChildForm())) return false;
			this.tableForm.lists.push({ checkYear: '', checkLevel: '' });
		},

		handleDeleteUser(index) {
			this.tableForm.lists.splice(index, 1);
		},

		validChildForm() {
			return new Promise((resolve) => {
				this.$refs['tableForm'].validate((valid) => {
					resolve(valid);
				});
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.custom_select_wrapper::v-deep {
	width: 100%;
	td.el-table__cell {
		padding: 0;
		.cell {
			padding: 0;
		}
	}
	.el-table__empty-block {
		min-height: 56px;
	}
	.el-table__empty-text {
		line-height: 56px;
	}
	.el-button.del {
		font-size: 16px;
	}
	.el-button.add {
		margin-top: 10px;
	}
	.el-form-item__content {
		padding: 10px;
	}
}
</style>
